/**
 * @description: el-form hook
 * @param {object} initialState form表单初始值
 * @param {object} config form表单配置
 * @return {object}
 */
export default function useForm(initValues = {}, options = {}) {
  const formRef = ref(null)
  const formData = ref({ ...initValues })
  const formConfig = {
    labelWidth: 90,
    labelPosition: 'left',
    ...options,
  }

  function setFormData(data) {
    formData.value = { ...data }
  }

  function getFormData() {
    return { ...formData.value }
  }

  async function validate() {
    await formRef.value.validate()
  }

  function clearValidate(prop) {
    formRef.value.clearValidate(prop)
  }

  function resetFields() {
    formRef.value.resetFields()
  }

  function getAllFields() {
    return formRef.value.fields
  }

  return {
    formRef,
    formData,
    formConfig,
    setFormData,
    getFormData,
    validate,
    clearValidate,
    resetFields,
    getAllFields,
  }
}
